package com.alibaba.citrus.ots.sales.plan.repository;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.alibaba.citrus.ots.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.ots.sales.plan.sdo.SalesPlanAuditLineSDO;

import com.epoch.app.bcots.sales_plan.salesplanauditdetail.dto.BatchCreateSalesPlanAuditDetailRequest;
import com.epoch.app.bcots.sales_plan.salesplanauditdetail.dto.BatchCreateSalesPlanAuditDetailResponse;
import com.epoch.app.bcots.sales_plan.salesplanauditdetail.dto.CreateSalesPlanAuditDetailResponse;
import com.epoch.app.bcots.sales_plan.salesplanauditdetail.model.SalesPlanAuditDetail;
import com.epoch.app.bcots.sales_plan.salesplanauditdetail.service.SalesPlanAuditDetailService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

@Component
public class SalesPlanAuditLineRepositoryImpl implements SalesPlanAuditLineRepository {

    @Resource
    SalesPlanAuditDetailService salesPlanAuditDetailService;

    @Override
    @RepositoryInvoker(printLog = true)
    public String save(SalesPlanAuditLineSDO salesPlanAuditLineSDO) {
        SalesPlanAuditDetail salesPlanAuditDetail = new SalesPlanAuditDetail();
        BeanUtils.copyProperties(salesPlanAuditLineSDO, salesPlanAuditDetail);
        salesPlanAuditDetail.setAuditOrderId(Long.valueOf(salesPlanAuditLineSDO.getAuditOrderId()));
        salesPlanAuditDetail.setPlanId(Long.valueOf(salesPlanAuditLineSDO.getPlanId()));
        salesPlanAuditDetail.setGmtCreate(new Date());
        salesPlanAuditDetail.setGmtModified(new Date());
        CreateSalesPlanAuditDetailResponse salesPlanAuditDetailResponse = salesPlanAuditDetailService.createSalesPlanAuditDetail(salesPlanAuditDetail);
        return salesPlanAuditDetailResponse.getLastInsertId();
    }

    @Override
    @RepositoryInvoker(printLog = true)
    public Boolean batchCreateSalesPlanAuditLine(List<SalesPlanAuditLineSDO> salesPlanLineAuditSDOList) {
        List<SalesPlanAuditDetail> salesPlanAuditDetails = salesPlanLineAuditSDOList.stream().map(item -> {
            SalesPlanAuditDetail salesPlanAuditDetail = new SalesPlanAuditDetail();
            BeanUtils.copyProperties(item, salesPlanAuditDetail);
            return salesPlanAuditDetail;
        }).collect(Collectors.toList());
        BatchCreateSalesPlanAuditDetailRequest batchCreateSalesPlanAuditDetailRequest = BatchCreateSalesPlanAuditDetailRequest.builder().salesPlanAuditDetails(salesPlanAuditDetails).build();
        BatchCreateSalesPlanAuditDetailResponse batchCreateSalesPlanAuditDetailResponse = salesPlanAuditDetailService.batchCreateSalesPlanAuditDetail(batchCreateSalesPlanAuditDetailRequest);
        return batchCreateSalesPlanAuditDetailResponse.getCount() > 0;
    }
}
